﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;


namespace STF.OrangeDog.Service
{
   
        public class BlogBll : IBlogBll
        {
            private IBlogDal _dal;

            public BlogBll(IBlogDal dal)
            {
            _dal = dal;
            }

            public async Task<int> AddBlogAsync(string Title, string Content, int Uid, DateTime PublishTime, int StateCode, int TypeId, int FbxsId, int LevelId, int GoodPraise, int NoGoodPraise)
            {
                return await _dal.AddModelAsync(new Blog
                {
                    Title = Title,
                    Content = Content,
                    uid = Uid,
                    PublishTime = PublishTime,
                    StateCode = StateCode,
                    typeId = TypeId,
                    fbxsId = FbxsId,
                    levelId = LevelId,
                    GoodPraise = GoodPraise,
                    NoGoodPraise = NoGoodPraise
                });
            }

            public async Task<int> DeleteBlogAsync(int Id)
            {
                var data = await _dal.GetDataByIdAsync(Id);
                if (data == null)
                    return -1;
                if (data.DeleteId == 1)
                    return 0;
                return await _dal.RemoveModelAsync(data);
            }

            public async Task<int> EditBlogAsync(int Id, string Title, string Content, int Uid, DateTime PublishTime, int StateCode, int TypeId, int FbxsId, int LevelId, int GoodPraise, int NoGoodPraise)
            {
                var data = await _dal.GetDataByIdAsync(Id);
                if (data == null)
                    return -1;
                data.Title = Title;
                data.Content = Content;
                data.uid = Uid;
                data.PublishTime = PublishTime;
                data.StateCode = StateCode;
                data.typeId = TypeId;
                data.fbxsId = FbxsId;
                data.levelId = LevelId;
                data.GoodPraise = GoodPraise;
                data.NoGoodPraise = NoGoodPraise;
                data.UpdateTime = DateTime.Now;
                return await _dal.EditModelAsync(data);
            }

            public async Task<IEnumerable<BlogDtos>> GetAll(int DeleteId = 1)
            {
                var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
                return data != null ? data.Select(r => new BlogDtos
                {
                    id = r.Id,
                    Title = r.Title,
                    Content = r.Content,
                    uid = r.uid,
                    PublishTime = r.PublishTime,
                    StateCode = r.StateCode,
                    typeId = r.typeId,
                    fbxsId = r.fbxsId,
                    levelId = r.levelId,
                    GoodPraise = r.GoodPraise,
                    NoGoodPraise = r.NoGoodPraise,
                   
                }) : new List<BlogDtos>();
            }

            public async Task<BlogDtos> GetDataById(int Id)
            {
                var data = await _dal.GetDataByIdAsync(Id);
                return data == null ? new BlogDtos() : new BlogDtos
                {
                    id = data.Id,
                    Title = data.Title,
                    Content = data.Content,
                    uid = data.uid,
                    PublishTime = data.PublishTime,
                    StateCode = data.StateCode,
                    typeId = data.typeId,
                    fbxsId = data.fbxsId,
                    levelId = data.levelId,
                    GoodPraise = data.GoodPraise,
                    NoGoodPraise = data.NoGoodPraise,
                   
                };
            }

            public async Task<IEnumerable<BlogDtos>> GetDataByTitle(string Title, int DeleteId = 1)
            {
                var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId && r.Title.Contains(Title));
                return data == null ? new List<BlogDtos>() :
                    data.Select(x => new BlogDtos
                    {
                        id = x.Id,
                        Title = x.Title,
                        Content = x.Content,
                        uid = x.uid,
                        PublishTime = x.PublishTime,
                        StateCode = x.StateCode,
                        typeId = x.typeId,
                        fbxsId = x.fbxsId,
                        levelId = x.levelId,
                        GoodPraise = x.GoodPraise,
                        NoGoodPraise = x.NoGoodPraise,
                       
                    });
            }
        }
}
